import server from '@/config/server/index.js'
import { userStore } from '@/store/user'

function post(path, auth = true, config = true) {
  return server.api().post(path).auth(auth).config(config)
}

function get(path, auth = true, config = true) {
  return server.api().get(path).auth(auth).config(config)
}

let api = {
  updatePwd: post('/sa-admin/api/v1/bs/noToken/updatePassword'),
  code: get('/sa-admin/api/v1/bs/noToken/sendMessage', false, false),
  updatePwdCode: post('/sa-admin/api/v1/bs/noToken/authCode')
}

function updatePwdCode(phone, codeOrPwd) {
  return api.updatePwdCode.fetch({ type: 1, phone, codeOrPwd })
}

function code(phone) {
  return api.code.fetch({ phone })
}

function updatePwd(params) {
  return api.updatePwd.fetch(params).then((res) => {
    userStore().update()
    return res
  })
}

export default {
  updatePwdCode,
  code,
  updatePwd
}
